package com.zzyl.controller;

import cn.hutool.core.lang.tree.Tree;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.service.ResourceService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.MenuVo;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/resource")
@Api(tags = "资源管理接口")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @PostMapping("/list")
    @ApiOperation("资源列表")
    @ApiImplicitParam(name = "resourceDto",value = "资源DTO",dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.resourceType","resourceDto.parentResourceNo"})
    public ResponseResult resourceList(@RequestBody ResourceDto resourceDto){
        List<ResourceVo> list=resourceService.resourceList(resourceDto);
        return ResponseResult.success(list);

    }

    @PostMapping("/tree")
    @ApiOperation("资源树形")
    @ApiImplicitParam(name = "resourceDto",value = "资源DTO",dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.label"})
    public ResponseResult<TreeVo> resourceTree(@RequestBody ResourceDto resourceDto){
        TreeVo treeVo=resourceService.resourceTree(resourceDto);
        return ResponseResult.success(treeVo);
    }

    @PutMapping
    @ApiOperation(value = "资源添加", notes = "资源添加")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState"
            , "resourceDto.icon"
            , "resourceDto.parentResourceNo"
            , "resourceDto.requestPath"
            , "resourceDto.resourceName"
            , "resourceDto.resourceType"
            , "resourceDto.sortNo"})
    public ResponseResult creatResource(@RequestBody ResourceDto resourceDto){
        resourceService.creatResource(resourceDto);
        return ResponseResult.success();
    }

    @PatchMapping
    @ApiOperation(value = "资源修改", notes = "资源修改")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState"
            , "resourceDto.icon"
            , "resourceDto.parentResourceNo"
            , "resourceDto.requestPath"
            , "resourceDto.resourceName"
            , "resourceDto.resourceType"
            , "resourceDto.sortNo"
            , "resourceDto.id"})
    public ResponseResult update(@RequestBody ResourceDto resourceDto){
        resourceService.update(resourceDto);
        return ResponseResult.success();

    }

    @PostMapping("/enable")
    @ApiOperation(value = "状态修改", notes = "状态修改")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState"
            , "resourceDto.parentResourceNo"
            , "resourceDto.sortNo"})
    public ResponseResult updateStatus(@RequestBody ResourceDto resourceDto){
        resourceService.updateStatus(resourceDto);
        return ResponseResult.success();
    }


    @DeleteMapping("/{resourceNo}")
    @ApiOperation(value = "资源删除", notes = "资源删除")
    public ResponseResult delete(@PathVariable String resourceNo){
        resourceService.delete(resourceNo);
        return ResponseResult.success();
    }

    @GetMapping("/menus")
    @ApiOperation("动态菜单")
    public ResponseResult<List<MenuVo>> menus(){
        //获取用户ID
        Long userId = UserThreadLocal.getMgtUserId();

        List<MenuVo> list=resourceService.menus(userId);
        return ResponseResult.success(list);
    }
}

